/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.SmsPackage;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.SmsPackageService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;


/**
 *
 *
 * @author lhd
 * @date 2020-07-04 09:54:16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/smsPackage" )
public class SmsPackageController {

    private final SmsPackageService smsPackageService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param smsPackage
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<SmsPackage>> getSmsPackagePage(PageParam<SmsPackage> page, SmsPackage smsPackage) {
        return ResponseEntity.ok(smsPackageService.page(page, new LambdaQueryWrapper<SmsPackage>().eq(SmsPackage::getIsDelete,0).orderByAsc(SmsPackage::getAmount)));
    }


    /**
     * 通过id查询
     * @param smsPackageId id
     * @return 单个数据
     */
    @GetMapping("/info/{smsPackageId}" )
    public ResponseEntity<SmsPackage> getById(@PathVariable("smsPackageId") Long smsPackageId) {
        return ResponseEntity.ok(smsPackageService.getById(smsPackageId));
    }

    /**
     * 新增
     * @param smsPackage
     * @return 是否新增成功
     */
    @SysLog("新增" )
    @PostMapping
    public ResponseEntity<Boolean> save(@RequestBody @Valid SmsPackage smsPackage) {
        if(smsPackageService.count(new LambdaQueryWrapper<SmsPackage>().eq(SmsPackage::getIsDelete,0)) > 10){
            // 套餐包数量已达上限十个，请进行删除后再添加
            throw new YamiShopBindException("yami.sys.sms.package.limit");
        }
        smsPackage.setIsDelete(0);
        smsPackage.setUpdateTime(new Date());
        return ResponseEntity.ok(smsPackageService.save(smsPackage));
    }

    /**
     * 修改
     * @param smsPackage
     * @return 是否修改成功
     */
    @SysLog("修改" )
    @PutMapping
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid SmsPackage smsPackage) {
        smsPackage.setIsDelete(0);
        smsPackage.setUpdateTime(new Date());
        return ResponseEntity.ok(smsPackageService.updateById(smsPackage));
    }

    /**
     * 通过id删除
     * @param smsPackageId id
     * @return 是否删除成功
     */
    @SysLog("删除" )
    @DeleteMapping("/{smsPackageId}" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long smsPackageId) {
        return ResponseEntity.ok(smsPackageService.update(new LambdaUpdateWrapper<SmsPackage>()
                .set(SmsPackage::getIsDelete,1)
                .set(SmsPackage::getUpdateTime,new Date())
                .eq(SmsPackage::getSmsPackageId,smsPackageId)));
    }

}
